<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}<#if moduleName?exists><#if moduleName!=''>.${moduleName}</#if></#if>.mapper.${entityName?cap_first}Mapper" >

	<sql id="Base_Column_List" >
		t.id,
		t.name,
		t.parent_id,
		t.parent_ids,
		p.name AS parent_name,
	   <#list columns as column>
	   <#if column.columnName!='id'&&column.columnName!='name'&& column.columnName!='parent_id'&& column.columnName!='parent_ids'>
	    t.${column.columnName}<#if !column.isBaseType> AS "${column.javaField}.id"</#if>,
	   </#if>
	   </#list>
	   case when c.id is null then 0 else 1 end as hasChildren
	</sql>
	
	<!-- 求子集 -->
	<sql id="childJoins"> 
		LEFT JOIN  (select distinct parent_id as id from ${tableName} ) c on c.id = t.id
	</sql>
	<sql id="parentJoins">
		LEFT JOIN (select distinct p1.parent_id as id,p2.name from ${tableName} p1 inner join ${tableName} p2 on p1.parent_id = p2.id ) p on t.parent_id = p.id
	</sql>
    <!-- 查找通过ID查找树信息 -->
	<select id="selectByTreeId" resultType="${entityName?cap_first}"
		parameterType="java.lang.String">
		select
		<include refid="Base_Column_List" />
		from ${tableName} t
		<include refid="childJoins"/>
		<include refid="parentJoins" />
		where t.id = ${"#"}{id,jdbcType=VARCHAR}
	</select>
	
	<!-- 查找树列表 -->
	<select id="selectTreeList" resultType="${entityName?cap_first}">
		select
		<include refid="Base_Column_List" />
		from ${tableName} t
		<include refid="childJoins"/>
		<include refid="parentJoins" />
		<where>
			${"$"}{ew.sqlSegment}
		</where>
	</select>

	<!-- 更新子树 -->
	<update id="updateSunTreeParentIds">
		update ${tableName} set parent_ids=CONCAT(${"#"}{newParentIds},
        <if test="'${r'${dbType}'}' == 'mysql'">
            substring(parent_ids, length(${"#"}{oldParentIds})+1,length(parent_ids)+1))
        </if>
        <if test="'${r'${dbType}'}' == 'oracle'">
            substr(parent_ids, length(${"#"}{oldParentIds})+1,length(parent_ids)+1))
        </if>
		where parent_ids like concat(${"#"}{oldParentIds}, '%')
	</update>
	
	<!-- 删除子树 -->
	<delete id="deleteSunTree" parameterType="java.lang.String">
		delete from ${tableName}
		where parent_ids like concat(${"#"}{oldParentIds}, '%')
	</delete>
	
	<!-- 批量插入 -->
  	<insert id="insertBatch">
  		insert into ${tableName} (<#list attributeInfos as column>${column.dbName}<#if column_has_next>,</#if></#list>)
    	values
    	<foreach collection ="list" item="item" separator =",">
         	(<#list attributeInfos as column>${"#"}{item.${column.name}}<#if column_has_next>,</#if></#list>)
    	</foreach >
  	</insert>	
  
  	<!-- 批量更新 -->
	<update id="updateBatch">
		update ${tableName} set 
		<#list attributeInfos as column>
		<#if column.dbName!='id'&&column.name!='createBy'&&column.name!='createDate'&&column.name!='updateBy'&&column.name!='updateDate'>
			${column.dbName} = 
			<foreach collection="list" item="item" index="index"
	            separator=" " open="case id" close="end">
	            when ${"#"}{item.id} then ${"#"}{item.${column.name}}
        	</foreach>
			<#if column_has_next>,</#if>
		</#if>
		</#list>
        where id in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            ${"#"}{item.id}
        </foreach>
	</update>
  
</mapper>